<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header">
    <h1>铆焊工时录入</h1>
</section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="sheetForm">
                        <div class="row">
                            <input type="hidden" name="reportType" value="1">
                            <input type="hidden" name="inputKind" value="0" >
                            <div class="form-inline">
                                <div class="form-group" style="padding-bottom:4px">
                                    <label class="control-label" style="text-align: right">项目编号</label>
                                    <label class="control-label">
                                        <select id="sheetForm-proCode" type="text" class="form-control" style="width: 150px" name="proCode" onchange="fetchPart()"></select>
                                    </label>
                                </div>
                                <div class="form-group" style="padding-bottom:4px">
                                    <label class="control-label">项目名称</label>
                                    <label class="control-label">
                                        <input id="sheetForm-proName" type="text" class="form-control" style="width: 280px" name="proName" readonly>
                                    </label>
                                </div>
                                <div class="form-group" style="padding-bottom:4px">
                                    <label class="control-label">项目数量</label>
                                    <label class="control-label">
                                        <input id="sheetForm-proNum" type="number" class="form-control" style="width: 80px" name="proNum" readonly>
                                        <input  id="sheetForm-proId" name="proId" type="hidden">
                                    </label>
                                </div>
                                <div class="form-group" style="padding-bottom:4px">
                                    <label class="control-label">设计专业</label>
                                    <label class="control-label">
                                        <select id="sheetForm-specialId" type="number" class="form-control" style="width: 150px" name="specialId" onchange="fetchPart()">>
                                            <option value= 0 >请选择设计专业</option>
                                            <option value= 60 >机械</option>
                                            <option value= 62 >自动化</option>
                                            <option value= 61 >流体</option>
                                        </select>
                                    </label>
                                </div>
                                <div class="form-group" style="padding-bottom:4px">
                                    <label class="control-label">设备部分</label>
                                    <label class="control-label">
                                        <select id="sheetForm-proPart" type="number" class="form-control"
                                                style="width: 150px" name="proPart">
                                        </select>
                                    </label>
                                </div>

                                <div id="toolbar" class="form-group" >
                                    <a href="javascript:void(0);"
                                       class="btn btn-success"
                                       onclick="solderingVerify()">
                                        <span class="Bold">数据确认</span>
                                    </a>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div id="toolbar1" class="row">
                    <a style="margin-right: 10px"
                       id="btn_add1" class="btn btn-info">
                        人工工时添加
                    </a>
                    <a href="javascript:void(0);"
                       style="margin-right: 10px"
                       class="btn btn-danger"
                       onclick="delWeldManHour()">
                        <span class="Bold">数据清空</span>
                    </a>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table1"
                           style="table-layout: fixed;word-break:break-all; word-wrap:break-word;"></table>
                </div>
                <div id="toolbar2" class="row">
                    <a style="margin-right: 10px"
                       id="btn_add2" class="btn btn-info">
                        辅材信息添加
                    </a>
                    <a href="javascript:void(0);"
                       style="margin-right: 10px"
                       class="btn btn-danger"
                       onclick="delWeldMat()">
                        <span class="Bold">数据清空</span>
                    </a>
                </div>
                <div class="box-body">
                    <table id="bootstrap-table2"
                           style="table-layout: fixed;word-break:break-all; word-wrap:break-word;"></table>
                </div>
            </div>
        </div>
    </div>
    <!--添加人工工时模态框-->
    <div class="modal fade" id="modal-add1">
        <div class="modal-dialog" style="width:900px">
            <div class="modal-content" style="height:430px">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4>
                        铆焊计划工时明细
                    </h4>
                </div>
                <div class="modal-body" style="height: 60%">
                    <form id="invForm1" class="form-horizontal" style="margin-right: 70px">
                        <div class="col-md-12" style="font-weight: bold">
                            <input type="hidden" name="kind" value="0" >
                            <input type="hidden" name="mName"value="1">
                            <input type="hidden" name="fkId">
                            <input type="hidden" name="fkCode">
                            <input type="hidden" name="partName">
                            <input type="hidden" name="specialId">
                            <input type="hidden" name="id">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="manHours">人工工时类别</label>
                                <div class="col-sm-10">
                                    <input type="text" id="manHours" name="manHours"
                                           class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="manNum">数量</label>
                                <div class="col-sm-10">
                                    <input type="text" id="manNum" name="manNum"
                                           class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="manUnit">单位</label>
                                <div class="col-sm-10">
                                    <input type="text" id="manUnit" name="manUnit"
                                           class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="manRemark">备注</label>
                                <div class="col-sm-10">
                                    <input type="text" id="manRemark" name="manRemark"
                                           class="form-control">
                                </div>
                            </div>
                        </div>

                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-info file-contract btn-file" id="btn-commit1">保存</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
    <!--添加辅材信息模态框-->
    <div class="modal fade" id="modal-add2">
        <div class="modal-dialog" style="width:900px">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">辅材信息编辑</h4>
                </div>
                <form id="invForm2" class="form-horizontal" style="margin-right: 70px">
                    <div class="modal-header" style="height: 300px">
                        <div class="col-md-12" style="font-weight: bold">
                            <input type="hidden" name="kind" value="0" >
                            <input type="hidden" name="mName"value="2">
                            <input type="hidden" name="fkId">
                            <input type="hidden" name="fkCode">
                            <input type="hidden" name="partName">
                            <input type="hidden" name="specialId">
                            <input type="hidden" name="mid">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="matName">辅材名称</label>
                                <div class="col-sm-10">
                                    <input type="text" id="matName" name="matName"
                                           class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="matNum">辅材用量</label>
                                <div class="col-sm-10">
                                    <input type="text" id="matNum" name="matNum"
                                           class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="matUnit">单位</label>
                                <div class="col-sm-10">
                                    <input type="text" id="matUnit" name="matUnit"
                                           class="form-control">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label class="col-sm-2 control-label no-padding-right" for="matRemark">备注</label>
                                <div class="col-sm-10">
                                    <input type="text" id="matRemark" name="matRemark"
                                           class="form-control">
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-info file-contract btn-file" id="btn-commit2">保存</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</section>

<div th:include="include :: footer"></div>
<script th:inline="javascript">
    var table1;
    var table2;


    /**
     * 工时删除
     * @param manId
     *
     */
    function manpowerErase(manId) {
        js.modal.confirm("确认删除该条人工工时信息吗？", function () {
            js.post({
                url: ctx + 'est/dataEntry/solderingPlan/delManHour',
                data: {
                    manId: manId
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table1);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        })
    }

    /**
     * 辅材删除
     * @param mid
     * */
    function materialExpurgate(mid) {
        js.modal.confirm("确认删除该条辅材信息吗？", function () {
            js.post({
                url: ctx + 'est/dataEntry/solderingPlan/delMaterial',
                data: {
                    mid: mid
                },
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table2);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });
    }
    /**
     * 人工工时数据清空
     *
     * */
    function delWeldManHour() {
        js.modal.confirm("确定清空所有人工工时数据吗？", function () {
            js.submit({
                data: new FormData($("#sheetForm")[0]),
                queryForm:"sheetForm",
                url: ctx + 'est/dataEntry/solderingPlan/delManHourAll',
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table1);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });
    }
    /**
     * 辅材信息数据清空
     *
     * */
    function delWeldMat() {
        js.modal.confirm("确定清空所有辅材信息数据吗？", function () {
            js.submit({
                data: new FormData($("#sheetForm")[0]),
                queryForm:"sheetForm",
                url: ctx + 'est/dataEntry/solderingPlan/delMatAll',
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table2);
                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });
    }

    /**
     * 工时编辑
     * @param id
     */
    function manpowerEdit(id) {

        js.post({
            url: ctx + "est/dataEntry/solderingPlan/oneManpowerTime",
            data: {
                id: id
            },
            success: function (result) {
                $("#invForm1").fillData(result);
                $("#id").val(id);
                $("#invForm1 input[name='mName']").val(1);
                js.modal.open("modal-add1");
                js.modal.closeLoading();
            }
        })
    }

    /**
     * 辅材编辑
     * @param mid
     */
    function materialRedact(mid) {
        $("#mid").val(mid);
        js.post({
            url: ctx + "est/dataEntry/solderingPlan/oneMaterial",
            data: {
                mid: mid
            },
            success: function (result) {
                $("#invForm2").fillData(result);
                $("#mid").val(mid);
                $("#invForm2 input[name='mName']").val(2);
                js.modal.open("modal-add2");
                js.modal.closeLoading();
            }
        })
    }

    $(function () {
        //加载列表数据,
        // 工时类别
        table1 = js.table.init({
            id: 'bootstrap-table1',
            queryForm:"sheetForm",
            url: ctx + "est/dataEntry/solderingPlan/manpower",
            toolbar: 'toolbar1',
            showColumns: false,
            showExport: false,
            pagination: false,
            pageSize: 3,
            striped: false,
            columns: [
                {
                    title: '序号', field: 'invId', width: '60',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '铆焊工时类别', field: 'manHours', width: '300'},
                {title: '数量', field: 'manNum', width: '120'},
                {title: '单位', field: 'manUnit', width: '120'},
                {title: '备注', field: 'manRemark', width: '300'},
                {title: '填报人', field: 'manInputUser', width: '120'},
                {title: '填报时间', field: 'manEditDate', width: '120'},
                {title: '接收状态', field: 'manReceiveFlag', width: '120',
                    formatter: function (value, row, index, field) {
                        if (value == 0) {
                            return '未接收';
                        } else if (value == 1) {
                            return '已接收';
                        }
                    }},
                {
                    title: '操作',
                    formatter: function (value, row, index) {
                        var actions = [];
                        actions.push("<a class='btn btn-warning btn-xs'  href='javascript:void(0);' onclick='manpowerEdit(" + row.id + ")'>编辑</a>");
                        actions.push("<a class='btn btn-danger btn-xs'  href='javascript:void(0);' onclick='manpowerErase(" + row.id + ")'>删除</a>");
                        return actions.join('');
                    }
                }
            ]
        });
        /**
         * 表格搜索
         */
        $("#btn-search").on('click', function () {
            js.table.search(table1);
        });
        //加载列表数据,辅材列表
        table2 = js.table.init({
            id: 'bootstrap-table2',
            queryForm:"sheetForm",
            data: new FormData($("#sheetForm")[0]),
            url: ctx + "est/dataEntry/solderingPlan/material",
            toolbar: 'toolbar2',
            showColumns: false,
            showExport: false,
            pagination: false,
            pageSize: 3,
            striped: false,
            columns: [
                {
                    title: '序号', field: 'invId', width: '60',
                    formatter: function (value, row, index) {
                        return index + 1;
                    }
                },
                {title: '主要辅材类别', field: 'matName', width: '300'},
                {title: '辅材用量', field: 'matNum', width: '120'},
                {title: '单位', field: 'matUnit', width: '120'},
                {title: '备注', field: 'matRemark', width: '300'},
                {title: '填报人', field: 'matInputUser', width: '120'},
                {title: '填报时间', field: 'matEditDate', width: '120'},
                {title: '接收状态', field: 'matReceiveFlag', width: '120',
                    formatter: function (value, row, index, field) {
                        if (value == 0) {
                            return '未接收';
                        } else if (value == 1) {
                            return '已接收';
                        }
                    }},
                {
                    title: '操作',
                    formatter: function (value, row, index) {

                        var actions = [];
                        actions.push("<a class='btn btn-warning btn-xs'  href='javascript:void(0);' onclick='materialRedact(" + row.mid + ")'>编辑</a>");
                        actions.push("<a class='btn btn-danger btn-xs'  href='javascript:void(0);' onclick='materialExpurgate(" + row.mid + ")'>删除</a>");
                        return actions.join('');
                    }
                }
            ]
        });
        /**
         * 表格搜索
         */
        $("#btn-search").on('click', function () {
            js.table.search(table2);
        });

        /**
         * 打开人工工时添加模态框
         */
        $("#btn_add1").on('click', function () {
            let validationPassed = true;
            let code = $("#sheetForm [name='proCode']").val();
            let special = $("#sheetForm [name='specialId']").val();
            let part = $("#sheetForm [name='proPart']").val();

            if (code === null || code.trim() === '') {
                js.modal.warning("请填写项目编号");
                validationPassed = false;
            }
            if (special == 0 || special.trim() === '') {
                js.modal.warning("请填写设计专业");
                validationPassed = false;
            }
            if (part === null || part.trim() === '') {
                js.modal.warning("请填写设备部分");
                validationPassed = false;
            }
            if(validationPassed){
                $("#invForm1 input[name='mName']").val(1);
                $("#invForm1 input[name='fkCode']").val($("#sheetForm select[name='proCode'] ").find(' option:selected').text());
                $("#invForm1 input[name='partName']").val($("#sheetForm select[name='proPart'] ").find(' option:selected').text());
                $("#invForm1 input[name='specialId']").val($("#sheetForm select[name='specialId'] ").val());
                $("#invForm1 input[name='fkId']").val($("#sheetForm input[name='proId']").val());
                $("#invForm1 input[name='kind']").val(0);
                js.modal.open("modal-add1");
            }

        });
        /**
         * 打开辅材添加模态框
         */
        $("#btn_add2").on('click', function () {
            let validationPassed = true;
            let code = $("#sheetForm [name='proCode']").val();
            let special = $("#sheetForm [name='specialId']").val();
            let part = $("#sheetForm [name='proPart']").val();

            if (code === null || code.trim() === '') {
                js.modal.warning("请填写项目编号");
                validationPassed = false;
            }
            if (special == 0 || special.trim() === '') {
                js.modal.warning("请填写设计专业");
                validationPassed = false;
            }
            if (part === null || part.trim() === '') {
                js.modal.warning("请填写设备部分");
                validationPassed = false;
            }
            if(validationPassed) {
                $("#invForm2 input[name='mName']").val(2);
                $("#invForm2 input[name='fkCode']").val($("#sheetForm select[name='proCode'] ").find(' option:selected').text());
                $("#invForm2 input[name='partName']").val($("#sheetForm select[name='proPart'] ").find(' option:selected').text());
                $("#invForm2 input[name='specialId']").val($("#sheetForm select[name='specialId'] ").val());
                $("#invForm2 input[name='fkId']").val($("#sheetForm input[name='proId']").val());
                $("#invForm2 input[name='kind']").val(0);
                js.modal.open("modal-add2");
            }
        });
        /**
         * 人工工时模态框保存
         */
        $("#btn-commit1").on('click', function () {
            js.submit({
                url: ctx + "est/dataEntry/solderingPlan/manpowerSave",
                data: new FormData($("#invForm1")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table1);
                        js.modal.hide("modal-add1");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });
        /**
         * 辅材信息模态框保存
         */
        $("#btn-commit2").on('click', function () {

            js.submit({

                url: ctx + "est/dataEntry/solderingPlan/manpowerSave",
                data: new FormData($("#invForm2")[0]),
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        $("#returnId").val(result.data);
                        js.modal.success(result.msg);
                        js.table.refresh(table2);
                        js.modal.hide("modal-add2");
                    } else {
                        js.modal.warning(result.msg);
                    }
                }
            });
        });
        /** 模态框隐藏事件，重置表单 */
        js.modal.hideEvent("modal-add1", function () {
            js.reset("invForm1");
        })
        js.modal.hideEvent("modal-add2", function () {
            js.reset("invForm2");
        })
        /** 项目编号下拉框 */
        js.select2({
            id: 'sheetForm-proCode',
            placeholder: '',
            ajax: {
                url: function () {
                    return ctx + 'est/dataEntry/solderingPlan/project/list'
                },
                processResults: function (data, page) {
                    let array = data;
                    for (let i = 0; i < data.length; i++) {
                        array[i]["id"] = array[i]['id'];
                        array[i]["text"] = array[i]['code'];
                    }
                    return {results: array};
                },
            }
        });
        /** 项目编号下拉框选中 */
        $("#sheetForm-proCode").on("select2:select", function () {
            $("#sheetForm-proName").val($(this).select2("data")[0].name);
            $("#sheetForm-proNum").val($(this).select2("data")[0].num);
            $("#sheetForm-proId").val($(this).select2("data")[0].id);
            getPart();
        });
        $("#sheetForm-specialId").change(function () {
            getPart();
        })
    });

    function solderingVerify() {

        js.modal.confirm("确定录入的数据是否正确？", function () {
            js.submit({
                data: new FormData($("#sheetForm")[0]),
                url: ctx + 'est/dataEntry/solderingPlan/solderingVerify',
                success: function (result) {
                    if (result.type === web_status.SUCCESS) {
                        js.modal.success(result.msg);
                        js.table.refresh(table1);
                        js.table.refresh(table2);
                        js.reset("sheetForm");
                        $("#sheetForm select[name='proCode']").val("");
                        $("#sheetForm select[name='proCode']").text("");

                    } else {
                        js.modal.warning(result.msg);
                    }
                    js.modal.closeLoading();
                }
            });
        });
    }
    /** 获取部分 */
    function getPart() {
        debugger;
        let code = $("#sheetForm-proCode").val();
        let type = $("#sheetForm-specialId").val();
        switch (type) {
            case '机械' :
            case '流体' :
            case '自动化' :
                fetchPart(code, type);
                break;
        }
    }
    function fetchPart() {
        let code = $("#sheetForm-proCode").find(' option:selected').text();
        let type = $("#sheetForm-specialId").val();
        if(code!=null && code!=undefined && type!=0){
            js.get({
                url: ctx + 'est/dataEntry/solderingPlan/part',
                data: {
                    code : code,
                    type : type
                },
                success: function (result) {
                    $("#sheetForm-proPart").empty().append(`<option value="">请选择</option>`);
                    result.forEach(value => {
                        if (value) {
                            $("#sheetForm-proPart").append(`<option value="${value.proPart}">${value.proPart}</option>`);
                        }
                    })

                }
            })
        }

    }
</script>
</body>
</html>